using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace closeApplication_cs
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Button btnCloseAll;
		internal System.Windows.Forms.ColumnHeader ColumnHeader2;
		internal System.Windows.Forms.ColumnHeader ColumnHeader1;
		internal System.Windows.Forms.Button btnLaunch1;
		internal System.Windows.Forms.ListView ListView1;
		internal System.Windows.Forms.Button btnClose1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private Process[] processes;
		private string procName = "Notepad";
		private string specialFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.System);

		public Form1()
		{
			// 
			// Required for Windows Form Designer support.
			// 
			InitializeComponent();
			// 
			// To do: Add any constructor code after the InitializeComponent call.
			// 
		}

		/// <summary>
		/// Clean up any resources that are being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnClose1 = new System.Windows.Forms.Button();
			this.ColumnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.ColumnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.btnCloseAll = new System.Windows.Forms.Button();
			this.btnLaunch1 = new System.Windows.Forms.Button();
			this.ListView1 = new System.Windows.Forms.ListView();
			this.SuspendLayout();
			// 
			// btnClose1
			// 
			this.btnClose1.Location = new System.Drawing.Point(160, 176);
			this.btnClose1.Name = "btnClose1";
			this.btnClose1.Size = new System.Drawing.Size(112, 32);
			this.btnClose1.TabIndex = 4;
			this.btnClose1.Text = "Close Selected Process";
			this.btnClose1.Click += new System.EventHandler(this.btnClose1_Click);
			// 
			// ColumnHeader2
			// 
			this.ColumnHeader2.Text = "Process ID";
			this.ColumnHeader2.Width = 85;
			// 
			// ColumnHeader1
			// 
			this.ColumnHeader1.Text = "Window Title";
			this.ColumnHeader1.Width = 160;
			// 
			// btnCloseAll
			// 
			this.btnCloseAll.Location = new System.Drawing.Point(160, 216);
			this.btnCloseAll.Name = "btnCloseAll";
			this.btnCloseAll.Size = new System.Drawing.Size(112, 32);
			this.btnCloseAll.TabIndex = 3;
			this.btnCloseAll.Text = "Close All Processes";
			this.btnCloseAll.Click += new System.EventHandler(this.btnCloseAll_Click);
			// 
			// btnLaunch1
			// 
			this.btnLaunch1.Location = new System.Drawing.Point(32, 176);
			this.btnLaunch1.Name = "btnLaunch1";
			this.btnLaunch1.Size = new System.Drawing.Size(112, 72);
			this.btnLaunch1.TabIndex = 1;
			this.btnLaunch1.Text = "Start Notepad";
			this.btnLaunch1.Click += new System.EventHandler(this.btnLaunch1_Click);
			// 
			// ListView1
			// 
			this.ListView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.ColumnHeader1,
																						this.ColumnHeader2});
			this.ListView1.Location = new System.Drawing.Point(22, 8);
			this.ListView1.MultiSelect = false;
			this.ListView1.Name = "ListView1";
			this.ListView1.Size = new System.Drawing.Size(250, 152);
			this.ListView1.TabIndex = 7;
			this.ListView1.View = System.Windows.Forms.View.Details;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCloseAll,
																		  this.btnLaunch1,
																		  this.ListView1,
																		  this.btnClose1});
			this.Name = "Form1";
			this.Text = "Process Example";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion
  
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
  
		private void buildList()
		{
			ListViewItem itemAdd ; 
			ListView1.Items.Clear();
			processes = Process.GetProcessesByName(procName);
			foreach (Process proc in processes)
			{
				itemAdd = ListView1.Items.Add(proc.MainWindowTitle);
				itemAdd.SubItems.Add(proc.Id.ToString());
			}
		}

		private void btnLaunch1_Click(object sender, System.EventArgs e)
		{
			ProcessStartInfo p = new ProcessStartInfo();
			p.FileName = specialFolder + @"\eula.txt";
			p.WindowStyle = ProcessWindowStyle.Minimized ;
			Process proc =Process.Start(p);
			proc.WaitForInputIdle();
			buildList();
		}
		private void Form1_Load(object sender, System.EventArgs e)
		{
      
		}

		private void btnClose1_Click(object sender, System.EventArgs e)
		{
			try
			{         
				int procID=System.Convert.ToInt32(ListView1.SelectedItems[0].SubItems[1].Text);
				Process tempProc=Process.GetProcessById(procID);
				tempProc.CloseMainWindow();
				tempProc.WaitForExit();
				buildList();
			}
			catch
			{
				MessageBox.Show("Please select a process in the ListView before clicking this button." +
					" Or the Process may have been closed by somebody." );
				buildList();
			}
		}

		private void btnCloseAll_Click(object sender, System.EventArgs e)
		{
			try
			{
				foreach (Process proc in processes)
				{
					proc.CloseMainWindow();
					proc.WaitForExit();
				}
				buildList();
			}
			catch (System.NullReferenceException)
			{
				MessageBox.Show("No instances of Notepad running.");
			}
		}

		private void closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			//Make sure that you do not leave any instances running.
			if (processes != null && processes.Length!=0)
				this.btnCloseAll_Click(this,e);
		}
	}
} 
